package com.robot.dao.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MyMeteObjecthandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object createTime = getFieldValByName("createTime", metaObject);
        if (createTime == null) {
            metaObject.setValue("createTime", LocalDateTime.now());
        }
        metaObject.setValue("updateTime", LocalDateTime.now());
//        Object viewTimes = getFieldValByName("viewTimes", metaObject);
//        if (createTime == null) {
//            metaObject.setValue("viewTimes", 0);
//        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
    }
}
